package com.wc.wcWebsite_model;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {
	public static void addCookie(HttpServletResponse response, String name, String value){
		Cookie cookie=new Cookie(name, value);
		cookie.setMaxAge(7*24*3600);
		cookie.setPath("/");
		response.addCookie(cookie);
	}
	
	public static Cookie getCookieByName(HttpServletRequest request, String name){
		Map<String, Cookie> cookieMap=getCookieMap(request);
		if(cookieMap != null && cookieMap.containsKey(name)){
			return cookieMap.get(name);
		}else{
			return null;
		}
	}
	
	public static Map<String,Cookie> getCookieMap(HttpServletRequest request){
		Map<String, Cookie> cookieMap=new HashMap<String, Cookie>();
		Cookie [] cookies=request.getCookies();
		if(cookies != null){
			for (Cookie cookie : cookies) {
				cookieMap.put(cookie.getName(), cookie);
			}
			return cookieMap;
		}else{
			return null;
		}
	}
	
}
